# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv

from utils.HttpUtils import HttpUtils

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


"""
东方财富股吧
http://gb.eastmoney.com/
"""


class DFCF():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            'User-Agent': ua.random,
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Safari/605.1.15',
            # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            # 'Accept-Encoding': 'gzip, deflate',
            # 'Accept-Language': 'zh-CN,zh;q=0.9',
            # 'Connection': 'keep-alive',
            # 'Cookie': 'qgqp_b_id=695bd57f116960a358c2320a724b9d2d; st_si=38354897868392; _adsame_fullscreen_18009=1; st_asi=delete; st_pvi=35829059509393; st_sp=2020-04-23%2009%3A46%3A19; st_inirUrl=http%3A%2F%2Fgb.eastmoney.com%2F; st_sn=9; st_psi=20200423095657238-117001301474-1345176348',
            # 'DNT': '1',
            # 'Host': 'gb.eastmoney.com',
            # 'Referer': 'http://gb.eastmoney.com/list,600507.html',
            # 'Upgrade-Insecure-Requests': '1',
            # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36'

        }

    # 获取贴吧列表
    def get_contents(self, cursor):
        postData = {

        }
        url = f"http://gb.eastmoney.com/list,600507_1.html"
        html = HttpUtils.do_request("GET", url, self.headers, "")
        root = etree.HTML(html.text)
        for r in root.xpath('//div[contains(@class,"articleh normal_post")]'):
            dict_data = dict()
            dict_data['read_count'] = r.xpath('./span[@class="l1 a1"]/text()')[0]  # 阅读量
            dict_data['comments_count'] = r.xpath('./span[@class="l2 a2"]/text()')[0]  # 评论量
            dict_data['title'] = r.xpath('./span[@class="l3 a3"]/a/@title')[0]  # 标题
            dict_data['child_url'] = "http://guba.eastmoney.com" + r.xpath('./span[@class="l3 a3"]/a/@href')[0]  # 链接
            dict_data['type'] = "".join(r.xpath('./span[@class="l3 a3"]/em/text()'))  # 类型 资讯、公告
            dict_data['owner'] = r.xpath('./span[@class="l4 a4"]/a/font/text()')[0]  # 作者
            dict_data['create_time'] = r.xpath('./span[@class="l5 a5"]/text()')[0]  # 时间
            print(str(dict_data))
            self.get_detail(dict_data['child_url'])

    # 获取标题信息
    def get_detail(self, url):
        html_child = HttpUtils.do_request("GET", url, self.headers, "")
        child_root = etree.HTML(html_child.text)
        for c in child_root.xpath('//div[@class="zwli clearfix"]'):
            username = c.xpath('.//div[@class="zwlianame"]/span/a/font/text()')[0]  # 用户
            star_count = "".join(c.xpath('.//div[@class="normal_user"]/span/@class'))  # 几星
            create_time = c.xpath('.//div[@class="zwlitime"]/text()')[0]  # 发布时间
            content = c.xpath('.//div[@class="short_text"]/text()')[0].replace("\r", "")  # 内容


if __name__ == '__main__':
    dfcf = DFCF()
    for i in range(1, 100):
        print(f"============{i}=============")
        # test_get.get_contents(i)
        dfcf.get_contents("")